﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DOTNET_ASSIGNMENT.Models;
using DOTNET_ASSIGNMENT.Services;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;
using System.Collections;

using System.Windows.Forms;
using System.IO;
namespace DOTNET_ASSIGNMENT.Controllers
{
    public class AdminCusController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /AdminCus/

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            CustomerServices customerServices = new CustomerServices();

            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }

            IEnumerable<Customer> query = customerServices.GetCustomers();
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            ViewBag.BlockSortParm = sortOrder == "Blocked" ? "blocked_desc" : "Blocked";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            if (!String.IsNullOrEmpty(searchString))
            {
                query = query.Where(s => s.Email.Contains(searchString));
            }

            switch (sortOrder)
            {
                case "Blocked":
                    query = query.OrderBy(s => s.IsBlocked);
                    break;
                case "blocked_desc":
                    query = query.OrderByDescending(s => s.IsBlocked);
                    break;
                case "name_desc":
                    query = query.OrderByDescending(s => s.Email);
                    break;
                case "Date":
                    query = query.OrderBy(s => s.Date);
                    break;
                case "date_desc":
                    query = query.OrderByDescending(s => s.Date);
                    break;
                default:
                    query = query.OrderBy(s => s.Email);
                    break;
            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);
            return View(query.ToPagedList(pageNumber, pageSize));
            //return View(db.Customers.ToList());
        }

        //
        // GET: /AdminCus/Details/5

        public ActionResult Details(int id = 0)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // GET: /AdminCus/Create

        public ActionResult Create()
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }
            return View();
        }

        //
        // POST: /AdminCus/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Customer customer)
        {

            if (ModelState.IsValid)
            {
                db.Customers.Add(customer);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customer);
        }

        //
        // GET: /AdminCus/Edit/5

        public ActionResult Edit(int id = 0)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // POST: /AdminCus/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Customer customer)
        {
            try
            {
                CustomerServices customerServices = new CustomerServices();
                if (customerServices.Edit(customer))
                {
                    return RedirectToAction("Index");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return View(customer);
        }

        //
        // GET: /AdminCus/Delete/5

        public ActionResult Delete(int id = 0)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // POST: /AdminCus/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Customer customer = db.Customers.Find(id);
            var files = db.Files.Where(x => x.CustomerEmail == customer.Email).ToList();

            //Xoa thong tin file
            foreach (var file in files)
            {
                db.Files.Remove(file);
                db.SaveChanges();
            }

            //Xoa folder chua trong data
            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/App_Data/Uploads/" + customer.Email));
            MessageBox.Show(di.ToString());
            if (di.Exists)
            {
                di.Delete(true);
            }

            //Xoa customer
            db.Set<Customer>().Remove(customer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Block(string email)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }
            CustomerServices customerServices = new CustomerServices();
            customerServices.Block(email);
            return RedirectToAction("Index");
        }

        public ActionResult Unblock(string email)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }
            CustomerServices customerServices = new CustomerServices();
            customerServices.UnBlock(email);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}